<!--基础 Styles-->
<link href="assets/css/bootstrap.min.css" rel="stylesheet"/>
<link href="assets/css/font-awesome.min.css" rel="stylesheet"/>
<!--自定义 styles-->
<link id="beyond-link" href="assets/css/beyond.min.css" rel="stylesheet" type="text/css"/>
<script src="${ctx}/assets/js/fuelux/treeview/tree-custom.min.js"></script>
<script src="assets/js/toastr/toastr.js"></script>
<script src="assets/js/beyond.min.js"></script>
<div class="row" style="margin: 0px; padding: 0px; background-color: white;">
    <div class="col-lg-12 col-xs-12" style="padding: 0px;margin: 0px;">
        <div class="widget flat radius-bordered" style="margin-top: 0px;">
            <input type="hidden" value="${projectDir}" id="projectDir">
            <div class="widget-body" style="overflow-x: hidden;padding: 0px; background-color: white;">
                <div id="projectDirTree" class="tree">
                    <div class="tree-folder" style="display: none;">
                        <div class="tree-folder-header">
                            <i class="fa fa-folder yellow"></i>
                            <div class="tree-folder-name"></div>
                        </div>
                        <div class="tree-folder-content"></div>
                        <div class="tree-loader" style="display: none;"></div>
                    </div>
                    <div class="tree-item" onclick="redFile(this)" style="display: none;">
                        <i class="tree-dot"></i>
                        <div class="tree-item-name"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    var AjaxDataSourceTree = function (options) {
        this._data = options.data;
        this._delay = options.delay;
        this._loadData = options.loadData;
        this._isInit = false;
    };
    AjaxDataSourceTree.prototype = {
        data: function (options, callback) {
            if (options.params && options.params.uuid) {
                console.log(options.params.uuid);
            }
            var self = this;
            if (self._isInit && self._loadData && (typeof self._loadData === "function")) {
                self._loadData(options, function (data) {
                    if (!data) {
                        data = [];
                    }
                    callback({data: data});
                });
            } else {
                setTimeout(function () {
                    var data = $.extend(true, [], self._data);
                    var childrenData = options.data;
                    if (childrenData) {
                        if (childrenData.length > 0) {
                            data = childrenData;
                        } else {
                            data = [];
                        }
                    }
                    callback({data: data});
                    self._isInit = true;
                }, this._delay);
            }
        }
    };
    /**
     * 延迟加载目录树
     */
    function loadAjaxTree(treeId, isIncludeFile, dir, setDataCallback) {
        var url = "codeGenerateController/redDiscJsonStr.do";
        if (!dir) {
            dir = "";
        }
        var params = {
            dir: dir,
            isIncludeFile: isIncludeFile
        };
        ajaxRequest(url, params, function (data) {
            if (data.resCode == "200") {
                var treeData = new Array();
                var treeDataOrg = eval("(" + data.data + ")");
                for (var i = 0, len = treeDataOrg.length; i < len; i++) {

                    var treeDataOrgItem = treeDataOrg[i];
                    var filePath = treeDataOrgItem.params.path;
                    if (/^.*\\WEB-INF\\(classes|lib|logs){1}.*$/.test(filePath)) {
                        continue;
                    }
                    var type = treeDataOrgItem.type;
                    if (type == "folder") {//文件夹
                        var name = '<div class="btn-group">' +
                            '<a class="btn btn-xs btn-default">' + treeDataOrgItem.name + '</a>' +
                            '<a class="btn btn-xs btn-sky dropdown-toggle" data-toggle="dropdown"><i class="fa fa-angle-down"></i></a>' +
                            '<ul class="dropdown-menu dropdown-yellow" role="menu">' +
                            '<li>' +
                            '<a href="#" onclick="mkdirFolder(\'' + filePath.replace(/\\/g, "/") + '\')"><i class="fa fa-plus"></i>&nbsp;&nbsp;创建文件夹</a>' +
                            '</li>' +
                            '<li>' +
                            '<a href="#" onclick="renameFolder(\'' + filePath.replace(/\\/g, "/") + '\')"><i class="fa fa-wrench warning"></i>&nbsp;&nbsp;重命名</a>' +
                            '</li>' +
                            '<li>' +
                            '<a href="#" onclick="deleteFolder(\'' + filePath.replace(/\\/g, "/") + '\')"><i class="fa fa-trash-o red"></i>&nbsp;&nbsp;删除文件夹</a>' +
                            '</li>' +
                            '<li class="divider">' +
                            '</li>' +
                            '<li>' +
                            '<a href="#" onclick="createFile(\'' + filePath.replace(/\\/g, "/") + '\')"><i class="fa fa-file-o"></i>&nbsp;&nbsp;新建文件</a>' +
                            '</li>' +
                            '<li>' +
                            '<a href="#" onclick="uploadFile(\'' + filePath.replace(/\\/g, "/") + '\')"><i class="fa fa-upload sky"></i>&nbsp;&nbsp;文件上传</a>' +
                            '</li>' +
                            '</ul>' +
                            '</div>';
                        treeDataOrgItem.name = name;
                    }
                    treeData.push(treeDataOrgItem);
                }
                console.log(treeData);
                if (setDataCallback && (typeof setDataCallback === "function")) {
                    setDataCallback(treeData);
                } else {
                    var discTreeAjaxDataSourceTree = new AjaxDataSourceTree({
                        data: treeData,
                        delay: 100,
                        loadData: function (option, callback) {
                            var nowDir = "";
                            console.log(option.params);
                            if (option.params && option.params.path) {
                                nowDir = option.params.path;
                            }
                            loadAjaxTree(treeId, isIncludeFile, nowDir, callback);
                        }
                    });
                    $("#" + treeId).tree({
                        cacheItems: false,
                        selectable: true,
                        dataSource: discTreeAjaxDataSourceTree,
                        loadingHTML: '<div class="tree-loading"><i class="fa fa-rotate-right fa-spin"></i></div>'
                    });
                }
            } else {
                ajaxError(data.resMsg);
            }
        }, function (XMLHttpRequest, textStatus, errorThrown) {
            ajaxError(errorThrown);
        }, "GET", "JSON");
    }
    //延迟加载项目结构树
    loadAjaxTree("projectDirTree", true, $("#projectDir").val());

    function redFile(iteam) {
        setTimeout(function () {
            loadFile();
        }, 100)
    }

    $(function(){
        $(".tree-item").mouseover(function(){
            $(this).find(".fileBtn").show();
            console.log(1);
        });
        $(".tree-item").mouseout(function(){
            $(this).find(".fileBtn").hide();
            console.log(2);
        });
    });

    function mkdirFolder(filePath) {
        bootbox.dialog({
            size: "small",
            title: "请输入文件夹名称",
            message: "<input type='text' id='folderName' class='form-control'>",
            buttons: {
                confirm: {
                    label: '创建',
                    className: 'btn-success',
                    callback: function (result) {
                        var folderName = $("#folderName").val();
                        if (folderName) {
                            console.log(folderName);
                            ajaxRequest("${ctx}/preserve/file/mkdirFolder.do", {
                                filePath: filePath,
                                folderName: folderName
                            }, function (data) {
                                if (data.resCode == "200") {
                                    Notify('保存成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                                } else {
                                    Notify(data.resMsg, 'bottom-right', '1000', 'success', 'fa-info', true);
                                }
                            }, function (XMLHttpRequest, textStatus, errorThrown) {
                                ajaxError(errorThrown);
                            }, "GET", "JSON");
                        } else {
                            Notify('请输入文件夹名称!', 'bottom-right', '1000', 'success', 'fa-info', true);
                        }
                    }
                },
                cancel: {
                    label: '取消',
                    className: 'btn-danger'
                }
            }
        });
    }
    function renameFolder(filePath) {
        bootbox.dialog({
            size: "small",
            title: "请输入文件夹名称",
            message: "<input type='text' id='folderName' class='form-control'>",
            buttons: {
                confirm: {
                    label: '重命名',
                    className: 'btn-success',
                    callback: function (result) {
                        var folderName = $("#folderName").val();
                        if (folderName) {
                            ajaxRequest("${ctx}/preserve/file/renameFolder.do", {
                                filePath: filePath,
                                folderName: folderName
                            }, function (data) {
                                if (data.resCode == "200") {
                                    Notify('保存成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                                } else {
                                    Notify(data.resMsg, 'bottom-right', '1000', 'success', 'fa-info', true);
                                }
                            }, function (XMLHttpRequest, textStatus, errorThrown) {
                                ajaxError(errorThrown);
                            }, "GET", "JSON");
                        }
                    }
                },
                cancel: {
                    label: '取消',
                    className: 'btn-danger'
                }
            }
        });
    }
    function deleteFolder(filePath) {
        bootbox.dialog({
            size: "small",
            title: "删除提示",
            message: "确认删除文件夹：<b>" + filePath + "</b>？",
            buttons: {
                confirm: {
                    label: '删除',
                    className: 'btn-success',
                    callback: function (result) {
                        ajaxRequest("${ctx}/preserve/file/deleteFolder.do", {
                            filePath: filePath
                        }, function (data) {
                            if (data.resCode == "200") {
                                Notify('删除成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                            } else {
                                Notify(data.resMsg, 'bottom-right', '1000', 'success', 'fa-info', true);
                            }
                        }, function (XMLHttpRequest, textStatus, errorThrown) {
                            ajaxError(errorThrown);
                        }, "GET", "JSON");
                    }
                },
                cancel: {
                    label: '取消',
                    className: 'btn-danger'
                }
            }
        });
    }
    function createFile(filePath) {
        bootbox.dialog({
            size: "small",
            title: "请输入文件名称",
            message: "<input type='text' id='fileName' class='form-control'>",
            buttons: {
                confirm: {
                    label: '创建',
                    className: 'btn-success',
                    callback: function (result) {
                        var fileName = $("#fileName").val();
                        if (fileName) {
                            ajaxRequest("${ctx}/preserve/file/createFile.do", {
                                filePath: filePath,
                                fileName: fileName
                            }, function (data) {
                                if (data.resCode == "200") {
                                    Notify('保存成功', 'bottom-right', '1000', 'success', 'fa-info', true);
                                } else {
                                    Notify(data.resMsg, 'bottom-right', '1000', 'success', 'fa-info', true);
                                }
                            }, function (XMLHttpRequest, textStatus, errorThrown) {
                                ajaxError(errorThrown);
                            }, "GET", "JSON");
                        }
                    }
                },
                cancel: {
                    label: '取消',
                    className: 'btn-danger'
                }
            }
        });
    }

    function uploadFile(filePath) {
        bootbox.dialog({
            size: "big",
            title: "请选择文件",
            buttons:{
                confirm:{
                    label: '取消',
                    className: 'hidden'
                },
            },
            message: '<iframe src="${ctx}/preserve/file/showUploadFile.do?filePath='+filePath+'" style="width:100%; min-height: 400px; border: none;"></iframe>',
        });
    }
</script>